/**
 * 费用详情管理
 */
var local_data = [
	{
		time: "2013-01-04  10:00",
		content: "陈XX发送了私信给张医师",
		money: "2元"
	}
];
/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess = false;
function turnDate(s) {
	var date = new Date(parseInt(s));
	return date.format("yyyy-MM-dd hh:mm");
}

var uid=$("#uid").val();


$( "#fund-detail" ).jqGrid({
//	datatype: "local",
//	data: local_data,
	url: '../manage/fundData.html?id='+uid ,
	datatype: "json",
	mtype: "POST",
	height: "100%", 
	colNames:[ 'first','邮箱', '昵称', '真实姓名',"手机号码", '性别', '血型', '二代身份证','id' , '消费金额','时间','事件'], 
	colModel:[
        {name:'first', index:'first', align:"center", sortable:false},    
	    {name:'email', index:'email', width:150, align:"center", hidden:true}, 
	    {name:'nickname', index:'nickname', width:100, align:"center", hidden:true}, 
		{name:'realName', index:'realName', width:100, align:"center", hidden:true},
		{name:'cellphone', index:'cellphone', width:100, align:"center", hidden:true},
		{name:'stringsex', index:'stringsex', width:50, align:"center", hidden:true}, 
		{name:'stringbloodType', index:'stringbloodType', width:100, align:"center", hidden:true},
		{name:'idNumber', index:'idNumber', width:100, align:"center", hidden:true},
		{name:'id',index:'id',hidden:true},
		{name:'changes', index:'changes', width:100, align:"center", hidden:true},
		{name:'createTime', index:'createTime',formatter : function(cellvalue, options, rowObj) {return turnDate(rowObj.createTime);}, width:100, align:"center", hidden:true},
		{name:'event', index:'event', width:100, align:"center", hidden:true}
	],
	rowNum: 20, 
	width:920,
	autowidth: false, 
	sortname: 'id',
	viewrecords: true, 
	sortorder: "desc",
	jsonReader: {
		root: "result",
		page: "page",
		total: "total",
		records: "records",
		repeatitems: false,
		cell: "cell",
		id: "id"
	},
	ondblClickRow: function(rowid) {
	},
	gridComplete:function() {
		// 取消外边框
		$( "#gbox_fund-detail" ).css( "border", "none" );
		
		// 隐藏表头
		$( "#gview_fund-detail .ui-jqgrid-hdiv" ).css( "display", "none" );
	
		// 取消列间的分界限
		$( "#gview_fund-detail .ui-jqgrid-bdiv td" ).css( "border", "0" );

		// 改变底边框样式
		$( "#gview_fund-detail .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
		
		// 取消最后一行的底边框
		$( "#gview_fund-detail .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
		
		// 设置表格行高
		//$( "#gview_fund-detail .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

		/**
		 * IDs 获取当前页所有数据的id
		 *
		 * @type {Object}
		 */

		var IDs = $( "#fund-detail" ).jqGrid( "getDataIDs" );
		
		for (var i=0; i < IDs.length; i++) {

			/**
			 * everyRowData 根据某一行数据的id获取该数据所有信息
			 *
			 * @type {Object}
			 */

			var everyRowData = $( "#fund-detail" ).jqGrid( "getRowData", IDs[i] );
			
			// 自定义模块
			var metadata ='<table class="per-con">' +
								'<tr>' +
									'<td width="150px" align="center" >' + everyRowData.createTime +'</td>'+
									'<td width="600px" align="left">' + everyRowData.event + '</td>' +
									'<td width="150px" align="left">' + everyRowData.changes + '</td>' +
								'</tr>' +
							'</table>';

			$( "#fund-detail" ).jqGrid( 'setRowData', IDs[i], { first: metadata } );
			// 取消提示信息
			$( ".metadata" ).parent("td").attr("title","");
		}
		/**
		 * records 得到总记录数
		 *
		 * @type {Number}
		 */

        var records =  $( "#fund-detail" ).getGridParam( "records");

        /**
		 * rowNum 得到一页显示的行数
		 *
		 * @type {Number}
		 */

        var rowNum =  $( "#fund-detail" ).getGridParam( "rowNum" );

        /**
		 * total 采用向上取整，计算出总页数
		 *
		 * @type {Number}
		 */

        var total = Math.ceil(records/rowNum);

        $( "#fund-detailPaging #total" ).text( total ); 

        $( "#fund-detailPaging #records" ).text( records );

        if ( total_last !== total ) {

			// 若总页码变化，则删除之前生成的页码
			$( '#fund-detailPaging a:not("#prev, #next")' ).remove();

			if ( total === 0 ){  // 全不等，防止类型转换
            	$( "#fund-detailPaging #prev" ).addClass( "disabled" );    // 向前向后按钮添加样式"disabled"
        		$( "#fund-detailPaging #next" ).addClass( "disabled" );
				$( "#fund-detailPaging #goto" ).val( "0" );  // 设置输入框的值为0
            } else if ( total === 1 ) {
				$( "#fund-detailPaging #next" ).before( '<a id="page">' + 1 + '</a>' );
				$( "#fund-detailPaging #prev" ).addClass( "disabled" );    // 向前向后按钮添加样式"disabled"
        		$( "#fund-detailPaging #next" ).addClass( "disabled" );
        		$( "#fund-detailPaging #next" ).prev().addClass( "curr" );
				$( "#fund-detailPaging #goto" ).val( "1" );  // 设置输入框的值为1
			} else if ( ( total > 1 ) && ( total <= 5 ) ) {   // 适用于总页数小于等于5的情况
        		$( "#fund-detailPaging #prev" ).removeClass( "disabled" );   // 向前向后按钮删除样式"disabled"
            	$( "#fund-detailPaging #next" ).removeClass( "disabled" );
        		for( var i = 1; i <= total; i++ ) {
            		$( "#fund-detailPaging #next" ).before( '<a id="page">' + i + '</a>' );
            	}
            	$( "#fund-detailPaging #prev" ).next().addClass( "curr" );   // 为第一页添加样式"curr"
            	$( "#fund-detailPaging #prev" ).addClass( "disabled" );    // 向前按钮添加样式"disabled"
				$( "#fund-detailPaging #goto" ).val( "1" );  // 设置输入框的值为1
            } else if ( total > 5 ) {
        		$( "#fund-detailPaging #prev" ).removeClass( "disabled" );   // 向前向后按钮删除样式"disabled"
        		$( "#fund-detailPaging #next" ).removeClass( "disabled" );
        		for( var i = 1; i <= 5; i++ ) {
        			$( "#fund-detailPaging #next" ).before( '<a id="page">' + i + '</a>' );
        		}
        		$( "#fund-detailPaging #prev" ).next().addClass( "curr" );   // 为第一页添加样式
            	$( "#fund-detailPaging #prev" ).addClass( "disabled" );    // 向前按钮添加样式"disabled"
				$( "#fund-detailPaging #goto" ).val( "1" );  // 设置输入框的值为1
        	}
			
			total_last = total;    // 改变total_last的值
		}
		
		loadDataSuccess = true;  // 改变loadDataSuccess的值，数据加载完毕
		
		// 点击页码触发
		$( '#fund-detailPaging a:not("#prev, #next")' ).bind( "click", function() {
			if ( loadDataSuccess ) {
				if ( parseInt( $(this).text() ) === 1 ) {
    				$( "#fund-detailPaging #prev" ).addClass( "disabled" );
    			}
    			
    			if ( parseInt( $(this).text() ) === total ) {
    				$( "#fund-detailPaging #next" ).addClass( "disabled" );							
    			}
    			
    			if ( parseInt( $(this).text() ) !== 1 ) {
    				$( "#fund-detailPaging #prev" ).removeClass( "disabled" );					
    			}
    			
    			if ( parseInt( $(this).text() ) !== total ) {
    				$( "#fund-detailPaging #next" ).removeClass( "disabled" );					
    			}
    			
    			// 当点击页码时，首先为选中的页码添加样式"curr"，然后将其他页码删除样式"curr"
    			$(this).addClass( "curr" ).siblings().removeClass( "curr" );

    			/**
    			 * pageNum 点击页码数
    			 *
    			 * @type {Number}
    			 */

    			var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

    			$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  //设置输入框的值为pageNum
			} else {
				// 如果数据未加载完就点击页码，则不做任何处理
			}
		});
	}
});

// 点击向前按钮
$( "#fund-detailPaging #prev" ).bind( "click", function() {
	if ( loadDataSuccess ) {

		/**
		 * currentPage 当前页
		 *
		 * @type {Number}
		 */

		var currentPage = parseInt( $("#fund-detailPaging .curr").text() );

		/**
		 * firstPage 第一个显示的页码数
		 *
		 * @type {Number}
		 */

		var firstPage = parseInt( $(this).next().text() );

		/**
		 * total 显示的总页码数
		 *
		 * @type {Number}
		 */

		var total = parseInt( $("#fund-detailPaging #total").text() );
	
		if ( total === 0 || total === 1 ) {
			return false;
		}
		
		// 当按钮后的页码为第1页时
		if ( firstPage === 1 ) {    
			if ( currentPage === 1 ) {
				return false;
			} else if ( currentPage === 2 ) {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).prev().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );

				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成

				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum

				$( "#fund-detailPaging #prev" ).addClass( "disabled" );
			} else {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).prev().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );

				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成

				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
			}
		} else {  // 当按钮后的页码不为第1页时
		
			// 当按钮后的页码就是当前页时
			if ( currentPage === firstPage ) {
				
				// 使每个页码的值都减1
				$( '#fund-detailPaging a:not("#prev, #next")' ).each(function() {
					$(this).text( parseInt( $(this).text() ) -1 );
				});

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );
				
				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
				
				// 如果当前页为第1页
				if ( pageNum === 1 ) {
					$( "#fund-detailPaging #prev" ).addClass( "disabled" );
				}
				
			} else {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).prev().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
			}
		}
		
		if ( parseInt( $("#fund-detailPaging .curr").text() ) !== total ) {
			$( "#fund-detailPaging #next" ).removeClass( "disabled" );
		}
	} else {
		
	}
});

// 点击向后按钮
$( "#fund-detailPaging #next" ).click(function() {
	if ( loadDataSuccess ) {

		/**
		 * currentPage 当前页
		 *
		 * @type {Number}
		 */

		var currentPage = parseInt( $("#fund-detailPaging .curr").text() );

		/**
		 * lastPage 最后一个显示的页码数
		 *
		 * @type {Number}
		 */

		var lastPage = parseInt( $(this).prev().text() );

		/**
		 * total 显示的总页码数
		 *
		 * @type {Number}
		 */

		var total = parseInt( $("#fund-detailPaging #total").text() );
		
		if ( total === 0 || total === 1 ) {
			return false;
		}
		
		// 当按钮前的页码为最后页时
		if ( lastPage === total ) {    
			if ( currentPage === total ) {
				return false;
			} else if ( currentPage === ( total-1 ) ) {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).next().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum

				$( "#fund-detailPaging #next" ).addClass( "disabled" );
			} else {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).next().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
			}
		} else { //当按钮前的页码不为最后页时
		
			// 当按钮前的页码就是当前页时
			if ( currentPage === lastPage ) {
				
				// 使每个页码的值都加1
				$( '#fund-detailPaging a:not("#prev, #next")' ).each(function() {
					$(this).text( parseInt( $(this).text() ) + 1 );
				});

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );
				
				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
				
				// 如果当前页为最后一页
				if ( pageNum === total ) {
					$( "#fund-detailPaging #next" ).addClass( "disabled" );
				}
			} else {
				$( "#fund-detailPaging .curr" ).removeClass( "curr" ).next().addClass( "curr" );

				/**
				 * pageNum class为curr的<a>标签代表的页码数
				 *
				 * @type {Number}
				 */

				var pageNum = parseInt( $("#fund-detailPaging .curr").text() );

				$( "#fund-detail" ).setGridParam( { page: pageNum } ).trigger( "reloadGrid" );
				
				loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成
				
				$( "#fund-detailPaging #goto" ).val( pageNum );  // 设置输入框的值为pageNum
			}
		}
		
		if ( parseInt( $("#fund-detailPaging .curr").text() ) !== 1 ) {
			$( "#fund-detailPaging #prev" ).removeClass( "disabled" );
		}
	} else {
		
	}
	
});

// 跳转处理事件
$( "#fund-detailPaging #goto" ).keypress(function(event) {
	if ( loadDataSuccess ) {
		if ( event.keyCode === 13 ) {

			/**
			 * total 得到总记录数
			 *
			 * @type {Number}
			 */

			var total = parseInt( $("#fund-detailPaging #total").text() );

			/**
			 * num 想要跳转到的页码数
			 *
			 * @type {Number}
			 */

			var num = parseInt( $("#fund-detailPaging #goto").val() ); 
			
			if ( total <= 0 ) {
				alert( "没有搜索结果！" );
			} else if ( total <= 5 && total > 0 ) {
				if ( num < 1 || num > total ) {
					alert( "请输入正确的页码数！" );
				} else {

					/**
					 * currentNum 重置页码数
					 *
					 * @type {Number}
					 */
 
						var currentNum = 0;

						$( '#fund-detailPaging a:not("#prev, #next")' ).each(function() {
						$(this).text( ++currentNum );
						if ( num === currentNum ) {
							$(this).addClass( "curr" ).siblings().removeClass( "curr" );
						}
					});
					
					$( "#fund-detail" ).setGridParam( { page: num } ).trigger( "reloadGrid" );

					loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成

					if ( num === 1 ) {
        				$( "#fund-detailPaging #prev" ).addClass( "disabled" );
        			}
        			
        			if ( num === total ) {
        				$( "#fund-detailPaging #next" ).addClass( "disabled" );							
        			}
        			
        			if ( num !== 1 ) {
        				$( "#fund-detailPaging #prev" ).removeClass( "disabled" );					
        			}
        			
        			if ( num !== total ) {
        				$( "#fund-detailPaging #next" ).removeClass( "disabled" );					
        			}
					
				}	
			} else if ( total > 5 ) {
				if ( num < 1 || num > total ) {
					alert( "请输入正确的页码数！" );
				} else if ( num <= 5 ) {

					/**
					 * currentNum 重置页码数
					 *
					 * @type {Number}
					 */
 
						var currentNum = 0;

						$( '#fund-detailPaging a:not("#prev, #next")' ).each(function() {
						$(this).text( ++currentNum );
						if ( num === currentNum ) {
							$(this).addClass( "curr" ).siblings().removeClass( "curr" );
						}
					});
					
					$( "#fund-detail" ).setGridParam( { page: num } ).trigger( "reloadGrid" );

					loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成

					if ( num === 1 ) {
        				$( "#fund-detailPaging #prev" ).addClass( "disabled" );
        			}
					
					if ( num !== 1 ) {
        				$( "#fund-detailPaging #prev" ).removeClass( "disabled" );					
        			}
					
					if ( num === total ) {
        				$( "#fund-detailPaging #next" ).addClass( "disabled" );							
        			}
					
					if ( num !== total ) {
        				$( "#fund-detailPaging #next" ).removeClass( "disabled" );					
        			}
					
				} else if ( num > 5 ) {

					/**
					 * currentNum 重置页码数
					 *
					 * @type {Number}
					 */

					var currentNum = num - 5;

					$( '#fund-detailPaging a:not("#prev, #next")' ).each(function() {
						$(this).text( ++currentNum );
					});
					
					$( "#fund-detailPaging #next" ).prev().addClass( "curr" ).siblings().removeClass( "curr" );

					$( "#fund-detail" ).setGridParam( { page: num } ).trigger( "reloadGrid" );

					loadDataSuccess = false;  // 改变loadDataSuccess的值，用于判定数据是否加载完成

					if ( num === 1 ) {
        				$("#fund-detailPaging #prev" ).addClass( "disabled" );
        			}
					
					if ( num !== 1 ) {
        				$( "#fund-detailPaging #prev" ).removeClass( "disabled" );					
        			}
					
					if ( num === total ) {
        				$( "#fund-detailPaging #next" ).addClass( "disabled" );							
        			}
					
					if ( num !== total ) {
        				$( "#fund-detailPaging #next" ).removeClass( "disabled" );					
        			}
				}
			}
		} else {
			
		}				
	}
});

/**
 * 实现删除功能
 */